﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DBA.Data;
using DBA.Data.Hibernate;
using DBA.Model;
using DBA.Common;

namespace Demo
{
    public partial class demo_page2Modify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            BindDate();
        }

        NHibernateQuery dal = CommonQuery.NHibernateQuery;

        private void BindDate()
        {
            string sql = "";
            IList<Condition> cons = new List<Condition>();
            ViewState["UserID"] = Request.Params["id"].Trim();
            cons.Add(new Condition("UserID",Op.Eq, ConvertHelper.ToInt(ViewState["UserID"])));
            Test_UserInfo userInfo = dal.GetOneObject<Test_UserInfo>(cons.ToArray());
            //dal.ManagerParameter.ClearParameter();
            //if (Request.Params["id"] != null)
            //{
            //    ViewState["UserID"] = Request.Params["id"].Trim();
            //    dal.ManagerParameter.AddNewInParameter("UserID", ViewState["UserID"].ToString());
            //    sql = "SELECT * FROM test_UserInfo WHERE UserID = @UserID";
            //    String[] values = dal.ExecuteStringsBySingleRow(sql);
            //    txtAddress.Text = values[2];
            //    txtEmail.Text = values[6];
            //    txtIDCard.Text = values[3];
            //    txtPhone.Text = values[4];
            //    txtPostalCode.Text = values[7];
            //    txtUserName.Text = values[1];
            //    txtMobile.Text = values[5];
            //}
                txtAddress.Text = userInfo.Address;
                txtEmail.Text = userInfo.Email;
                txtIDCard.Text = userInfo.IDcard;
                txtPhone.Text = userInfo.Phone;
                txtPostalCode.Text = userInfo.PostalCode;
                txtUserName.Text = userInfo.UserName;
                txtMobile.Text = userInfo.Mobile;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

        }

        protected void lbtn_Save_Click(object sender, EventArgs e)
        {
            Test_UserInfo userInfo = new Test_UserInfo();
            userInfo.UserName = txtUserName.Text.Trim();
            userInfo.Address = txtAddress.Text.Trim();
            userInfo.IDcard = txtIDCard.Text.Trim();
            userInfo.Phone = txtPhone.Text.Trim();
            userInfo.Mobile = txtMobile.Text.Trim();
            userInfo.Email = txtEmail.Text.Trim();
            userInfo.PostalCode = txtPostalCode.Text.Trim();
            userInfo.UserID = ConvertHelper.ToInt(ViewState["UserID"]);

            //dal.ManagerParameter.ClearParameter();
            //dal.ManagerParameter.AddNewInParameter("UserName", txtUserName.Text.Trim());
            //dal.ManagerParameter.AddNewInParameter("Address", txtAddress.Text.Trim());
            //dal.ManagerParameter.AddNewInParameter("IDcard", txtIDCard.Text.Trim());
            //dal.ManagerParameter.AddNewInParameter("Phone", txtPhone.Text.Trim());
            //dal.ManagerParameter.AddNewInParameter("Mobile", txtMobile.Text.Trim());
            //dal.ManagerParameter.AddNewInParameter("Email", txtEmail.Text.Trim());
            //dal.ManagerParameter.AddNewInParameter("PostalCode", txtPostalCode.Text.Trim());
            //dal.ModifyData.Update("test_UserInfo", "UserID=" + ViewState["UserID"].ToString());
            if (dal.Update<Test_UserInfo>(userInfo))
            {
                ScriptManager.RegisterStartupScript(this, base.GetType(), "updateOk" + System.Guid.NewGuid().ToString(), "Public.OpertAlert('数据修改成功',true);", true);
            }
            else
            {
            }
        }
    }
}